﻿using System;
using System.IO;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;

namespace DevelopTool.Forms.EncodOrEncrypForms
{
    /// <summary>
    /// Base64图片编码/解码
    /// </summary>
    public partial class Base64ImageForm : BaseUIPage
    {
        public Base64ImageForm()
        {
            InitializeComponent();
        }

        private void Base64ImageForm_Load(object sender, EventArgs e)
        {
        }


        /// <summary>
        /// 图片转为Base64文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEncode_Click(object sender, EventArgs e)
        {
            if (this.pictureBox.Image == null)
            {
                this.ShowWarningNotifier("请打开需要转换的图片");
                return;
            }

            try
            {
                this.codeEditorControl.ContentText = ImageHelper.ConvertImageToBase64(pictureBox.Image);

                this.ShowWarningTip(this.btnEncode, "已转为Base64");

            }
            catch (Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }
        }

        /// <summary>
        /// Base64文本转为图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDecode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.codeEditorControl.ContentText))
            {
                this.ShowWarningNotifier("请输入Base64文本");
                return;
            }

            try
            {
                this.pictureBox.Image = ImageHelper.ConvertBase64ToImage(this.codeEditorControl.ContentText);
                this.ShowWarningTip(this.btnDecode, "已转为图片");
            }
            catch (Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }

        }

        /// <summary>
        /// 打开图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenPicture_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            if (!Dialogs.OpenFileDialog(ref fileName,ImageHelper.GetImageFilter())) return;
            if (string.IsNullOrEmpty(fileName)) return;
            if (!File.Exists(fileName)) return;
            
            try
            {
                this.pictureBox.Image = ImageEx.FromFile(fileName);
            }
            catch (Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }
        }



        /// <summary>
        /// 图片另存为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSavePicture_Click(object sender, EventArgs e)
        {
            if (this.pictureBox.Image == null) return;
            string fileName = string.Empty;

            if (!Dialogs.SaveFileDialog(ref fileName,ImageHelper.GetImageFilter())) return;
            if (string.IsNullOrEmpty(fileName)) return;

            try
            {
                this.pictureBox.Image.Save(fileName);
                this.ShowSuccessNotifier("图片已保存到:" + fileName);
            }
            catch (Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }
        }

        private void Base64ImageForm_Resize(object sender, EventArgs e)
        {
            #region 按钮居中
            int horizontalCenter = this.uiSplitContainer1.Panel1.Left + this.uiSplitContainer1.Panel1.Width / 2;
            this.btnDecode.Location = new System.Drawing.Point(horizontalCenter - (this.btnDecode.Width / 2), this.btnDecode.Location.Y);


            int horizontalCenter2 = this.uiSplitContainer1.Panel2.Left + this.uiSplitContainer1.Panel2.Width / 2;
            this.btnEncode.Location = new System.Drawing.Point(horizontalCenter2 - (this.btnEncode.Width / 2), this.btnEncode.Location.Y);
            #endregion 按钮居中
        }
    }
}
